#include<vector>
#include<deque>
#include<unordered_set>
using namespace std;
class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        int n = rooms.size();
        unordered_set<int>visited;
        deque<int>queue;
        queue.push_back(0);
        visited.insert(0);
        while (!queue.empty()) {
            int index = queue.front();
            queue.pop_front();
            for (auto tmp : rooms[index]) {
                if (visited.find(tmp) != visited.end())
                    continue;
                queue.push_back(tmp);
                visited.insert(tmp);
            }
        }
        for (int i = 0; i < n; ++i) {
            if (visited.find(i) == visited.end())
                return false;
        }
        return true;
    }
};